﻿//using System.Linq;
//using System.Windows;
//using System.Windows.Controls;
//using System.Windows.Media;
//using System.Windows.Media.Effects;
//using Oliver.Behaviors;

//namespace Oliver.Controls
//{
//    public class ImageGrid : BaseContainer
//    {
//        private double imageWidth
//        {
//            get { return (double)this.GetValue(ImageWidthProperty); }
//        }

//        private double imageHeight 
//        {
//            get { return (double)this.GetValue(ImageHeightProperty); }
//        }

//        private int rowsPerColumn
//        {
//            get { return (int)this.GetValue(RowsPerColumnProperty); }
//        }

//        private double imageMargin
//        {
//            get { return (double)this.GetValue(ImageMarginProperty); }
//        }

//        protected override Size MeasureOverride(Size availableSize)
//        {
//            double width = ((this.Children.Count % rowsPerColumn) + 1) * (imageWidth + imageMargin);
//            double height = rowsPerColumn * (imageHeight + imageMargin);

//            Size newSize = new Size(width, height);

//            return base.MeasureOverride(newSize);
//        }

//        protected override Size ArrangeOverride(Size finalSize)
//        {
//            // Clipping
//            RectangleGeometry cliprect = new RectangleGeometry();
//            cliprect.Rect = new Rect(0, 0, finalSize.Width, finalSize.Height);
//            this.Clip = cliprect;

            
//            int currentRow = 0;
//            int currentColumn = 0;

//            foreach (Image image in this.Children.OfType<Image>())
//            {
//                Hover3DBehavior behavior = new Hover3DBehavior();
//                behavior.ZHoverValue = (double)this.GetValue(ZHoverValueProperty);
//                behavior.Attach(image);

//                if ((bool)this.GetValue(ShowShadowProperty))
//                {
//                    DropShadowEffect ds = new DropShadowEffect();
//                    ds.Opacity = .3;
//                    ds.BlurRadius = 10.0;
//                    ds.Direction = 0.0;
//                    ds.ShadowDepth = 20.0;
//                    image.Effect = ds;
//                }

//                Rect rect = new Rect(
//                    currentColumn * (imageWidth + imageMargin),
//                    currentRow * (imageHeight + imageMargin),
//                    imageWidth,
//                    imageHeight);

//                image.Width = imageWidth;
//                image.Height = imageHeight;
//                image.Arrange(rect);
//                image.InvalidateArrange();

//                currentRow++;
//                if (currentRow >= rowsPerColumn)
//                {
//                    currentRow = 0;
//                    currentColumn++;
//                }
//            }

//            Size newSize = new Size((currentColumn+1) * (imageWidth + imageMargin), rowsPerColumn * (imageHeight+imageMargin));

//            return base.ArrangeOverride(newSize);
//        }

//        private const int C_DefaultRowsPerColumn = 5;
//        private const double C_DefaultImageWidth = 160.0;
//        private const double C_DefaultImageHeight = 120.0;
//        private const double C_DefaultZHover = 200.0;
//        private const bool C_DefaultShowShadow = true;
//        private const double C_DefaultImageMargin = 8.0;

//        #region Images per column

//        public int RowsPerColumn
//        {
//            get { return (int)GetValue(RowsPerColumnProperty); }
//            set { SetValue(RowsPerColumnProperty, value); }
//        }

//        public static readonly DependencyProperty RowsPerColumnProperty =
//                DependencyProperty.Register("RowsPerColumn", typeof(int),
//                typeof(ImageGrid),
//                new PropertyMetadata(C_DefaultRowsPerColumn,
//                new PropertyChangedCallback(OnImageSizeChanged)));

//        #endregion

//        #region Image Width

//        public double ImageWidth
//        {
//            get { return (double)GetValue(ImageWidthProperty); }
//            set { SetValue(ImageWidthProperty, value); }
//        }

//        public static readonly DependencyProperty ImageWidthProperty =
//                DependencyProperty.Register("ImageWidth", typeof(double),
//                typeof(ImageGrid),
//                new PropertyMetadata(C_DefaultImageWidth,
//                new PropertyChangedCallback(OnImageSizeChanged)));

//        #endregion

//        #region Image Margin

//        public double ImageMargin
//        {
//            get { return (double)GetValue(ImageMarginProperty); }
//            set { SetValue(ImageMarginProperty, value); }
//        }

//        public static readonly DependencyProperty ImageMarginProperty =
//                DependencyProperty.Register("ImageMargin", typeof(double),
//                typeof(ImageGrid),
//                new PropertyMetadata(C_DefaultImageMargin,
//                new PropertyChangedCallback(OnImageSizeChanged)));

//        #endregion

//        #region Image Height

//        public double ImageHeight
//        {
//            get { return (double)GetValue(ImageHeightProperty); }
//            set { SetValue(ImageHeightProperty, value); }
//        }

//        public static readonly DependencyProperty ImageHeightProperty =
//                DependencyProperty.Register("ImageHeight", typeof(double),
//                typeof(ImageGrid),
//                new PropertyMetadata(C_DefaultImageHeight,
//                new PropertyChangedCallback(OnImageSizeChanged)));

//        protected static void OnImageSizeChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
//        {
//            ImageGrid i = obj as ImageGrid;
//            i.InvalidateArrange();
//        }

//        #endregion

//        #region ZHoverValue 

//        public double ZHoverValue
//        {
//            get { return (double)GetValue(ZHoverValueProperty); }
//            set { SetValue(ZHoverValueProperty, value); }
//        }

//        public static readonly DependencyProperty ZHoverValueProperty =
//            DependencyProperty.Register("ZHoverValue", typeof(double), typeof(ImageGrid),
//            new PropertyMetadata((double)C_DefaultZHover, null));

//        #endregion

//        #region Show Shadow

//        public bool ShowShadow
//        {
//            get { return (bool)GetValue(ShowShadowProperty); }
//            set { SetValue(ShowShadowProperty, value); }
//        }

//        public static readonly DependencyProperty ShowShadowProperty =
//            DependencyProperty.Register("ShowShadow", typeof(bool), typeof(ImageGrid),
//            new PropertyMetadata((bool)C_DefaultShowShadow, new PropertyChangedCallback(OnImageSizeChanged)));

//        #endregion

//    }
//}
